package frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import commans.ErrorPage;

import entity.FirePolicy;
import entity.FirePolicyEntity;
import far.FirePolicyGType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 住火個人家庭傷害險要保書承式進入點
 */
public class FireGtypeFrame extends JFrame
{

    private JPanel contentPane;
    private File insertFile = null;
    private int ret = 1;
    private JFileChooser fc = new JFileChooser();

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
	  public void run()
	  {
	      try
	      {
		FireGtypeFrame frame = new FireGtypeFrame();
		frame.setVisible(true);
	      }
	      catch (Exception e)
	      {
		new ErrorPage(e);
	      }
	  }
        });
    }

    /**
     * Create the frame.
     */
    public FireGtypeFrame()
    {
        setTitle("屋主家庭成員綜合險");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String icon = "Logo.PNG";
        ImageIcon img = new ImageIcon(getClass().getResource("/"+icon));
        setIconImage(img.getImage());
        setBounds(100,100,263,90);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JButton btnNewButton = new JButton("開啟檔案");
        setLocationRelativeTo(null); // 視窗至中
        setResizable(false);
        btnNewButton.addActionListener(new ActionListener()
        {
	  public void actionPerformed(ActionEvent arg0)
	  {
	      fc.setCurrentDirectory(new java.io.File("C:\\workf"));
	      ret = fc.showOpenDialog(null);
	      insertFile = fc.getSelectedFile();
	  }
        });
        btnNewButton.setBounds(14,13,101,29);
        contentPane.add(btnNewButton);
        JButton btnNewButton_1 = new JButton("產生");
        btnNewButton_1.addActionListener(new ActionListener()
        {
	  public void actionPerformed(ActionEvent arg0)
	  {
	      if(ret==JFileChooser.APPROVE_OPTION)
	      {
		FirePolicy firepolicy = new FirePolicy();
		try
		{
		    firepolicy.getPolicy(insertFile.getAbsolutePath());
		}
		catch (Exception e)
		{
		    new ErrorPage(e);
		}
		List<FirePolicyEntity> policys = firepolicy.SortPolicy();
		new Thread(new FirePolicyGType(policys),"CreatePolicy").start();
	      }
	      else
	      {
		JOptionPane.showMessageDialog(null,"無匯入檔案");
	      }
	  }
        });
        btnNewButton_1.setBounds(131,13,101,29);
        contentPane.add(btnNewButton_1);
    }
}
